package main.orga;

import java.util.List;
import java.util.Map;

import main.util.JdbcUtil;

public class InitOrgaIdAndCode {
	//根节点的old_orgaId
	static String old_rootId    = "1000";
	//根节点的新组织机构id（根据情况自己定义）
	static String newOrgaId     = "000300260107";
	//根节点的新组织机构code（根据情况自己定义）
	static String newOrgaCode   = "001026107";
	//根节点要挂在已有的哪个组织机构下
	static String superOrgaCode = "001026";

	static JdbcUtil jdbcUtil;
	public static void main(String[] args) {
		
		jdbcUtil = new JdbcUtil();
		String sql = "SELECT a.old_orgaId FROM t_kfrh_orgainfo_gx a WHERE a.old_orgaId = '"+old_rootId+"'";
		List<Map<String, Object>> rootInfoList = jdbcUtil.queryForList(sql);
		if (rootInfoList.size() != 1) {
			System.out.println("根节点信息有误！old_rootId="+old_rootId);
			return;
		}
		
		initOrgaInfo(old_rootId,newOrgaId,newOrgaCode,superOrgaCode);
	}

	private static void initOrgaInfo(String old_rootId2, String newOrgaId2,
			String newOrgaCode2, String superOrgaCode2) {
		String sql = "update t_kfrh_orgainfo_gx set new_orgaId='"+newOrgaId2
				+"', new_orgacode='"+newOrgaCode2
				+"', new_superorgacode='"+superOrgaCode2
				+"' where old_orgaId='"+old_rootId2+"';";
		System.out.println(sql);
		jdbcUtil = new JdbcUtil();
		jdbcUtil.update(sql);
		
		sql = "SELECT a.old_orgaId FROM t_kfrh_orgainfo_gx a WHERE a.old_superorgaId = '"+old_rootId2+"'";
		System.out.println(sql);
		jdbcUtil = new JdbcUtil();
		List<Map<String, Object>> childList = jdbcUtil.queryForList(sql);
		for (int i = 1; i <= childList.size(); i++) {
			String temp = ""+i;
			String old_rootId3 = (String)childList.get(i-1).get("old_orgaId");
			String newOrgaId3 = newOrgaId2+"0000".substring(0,4-temp.length())+temp;
			String superOrgaCode3 = newOrgaCode2;
			System.out.println("temp:"+temp+"; old_rootId3"+old_rootId3+"; newOrgaCode2:"+newOrgaCode2);
			String newOrgaCode3 = newOrgaCode2+"000".substring(0,3-temp.length())+temp;
			initOrgaInfo(old_rootId3, newOrgaId3, newOrgaCode3, superOrgaCode3);
		}
		
	}
}
